// data_stuctures_txn.cpp
// Customizer_Base
// Protea Customizer/Customizer Pro / Business Logic for managing data structures (buffers)
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Please send correspondence to:
//
// theproteaproject@gmail.com
//

#include "base_setup.h"
#include "../../../Fields/field_defns.h"
#include <iostream>


//
//@ CHA RevDataStructure
//@ COD RevDataStructure
//@ NUM 190
//@ CRI FORMAT_NAME (or number)
//@ TAB FORMAT_DESCRIPTR Read
//@ TAB FIELD_DESCRIPTOR Read 
//@ TAB DATA_STRUCTURES Read
//
//@ GEN RevDataStructure will review all field records in a particular format
//
TRANSACTION_CODE( RevDataStructure ) {
	XCBuffer* detail = NULL;
	XCBuffer* TransBuf = NULL;
	XCBuffer* format_buffer = NULL;
	XCDBStatement* stmt = NULL;

	START_TXN( RevDataStructure );
		//@ Create a DATA_STRUCTURES buffer
		TransBuf = App->CreateBuffer( FMT_DATA_STRUCTURES, false );

		//@ Move supplied buffer information into DATA_STRUCTURES buffer
		TransBuf->MoveCorresponding( txnvars.buffer, txnvars.Errors );

		//@ Check for validation errors
		txnvars.Errors->Check();

		//@ Create a FORMAT_DESCRIPTOR buffer
		format_buffer = App->CreateBuffer( FMT_FORMAT_DESCRIPTR, true );

		START_DB_TXN();
			//@ SUB DSGetFieldFormatNames to translate format name into a format number
			DSGetFieldFormatNames( &txnvars, TransBuf, false, format_buffer );
			txnvars.Errors->Check();

			txnvars.buffer->DebugDump();
			TransBuf->DebugDump();
			format_buffer->DebugDump();

			//@ Check if DATA_STRUCTURES record exists
			stmt = DataStructureReviewFormat( TransBuf, &txnvars );

			//@ Move data from DATA_STRUCTURES record back to the screen
			txnvars.buffer->MoveCorresponding( TransBuf, txnvars.Errors );

			//@ Move FORMAT_TYPE from format_buffer into DATA_TYPE
			txnvars.buffer->MoveUnlikeField( FLD_DATA_TYPE, format_buffer, FLD_FORMAT_TYPE );
			txnvars.Errors->Check();

			//@ Send the header buffer back to the host
			App->TransResponse( txnvars.result, txnvars.buffer, TRANS_HEADER_TYPE );

			if (!stmt->Eof()) {
				int child = txnvars.buffer->GetChildFormat();
				if (child) {
					//@ Create the detail buffer
					detail = App->CreateBuffer( child, false );

					//@ For each DATA_STRUCTURE record:
					while (!stmt->Eof()) {
						//@ GRS
						detail->MoveCorresponding( TransBuf, txnvars.Errors );
						txnvars.Errors->Check();

						//@ SUB LookupNameFromNumber to translate number to name
						LookupNameFromNumber( &txnvars, TransBuf, detail, FLD_FIELD_NUMBER, FLD_FIELD_NAME, FMT_FIELD_DESCRIPTOR );
						txnvars.Errors->Check();

						//@ Return detail buffer information to host
						App->TransResponse( txnvars.result, detail, TRANS_DETAIL_TYPE );
						stmt->MoveNext();
						//@ GRE
					};
				};
			};
		END_DB_TXN();

	CLEANUP_TXN()
		App->CleanupObjects( 4, &TransBuf, &detail, &format_buffer, &stmt );
	END_TXN();
};
